#include "Book.h"
#include <string.h>

#pragma warning( disable : 4996 )

Book::Book(const char *pBookName, const int numPages )
{
	m_pName = new char[ strlen(pBookName) + 1 ];
	strcpy( m_pName, pBookName );
	m_numPages = numPages;
}

Book::Book( const Book &book )
{
	m_pName = new char[ strlen(book.m_pName) + 1 ];
	strcpy( m_pName, book.m_pName );
	m_numPages  = book.m_numPages;
}

Book::~Book( void )
{
	delete [] m_pName;
}

// This is called shallow copy
// NOTE: If you don't define the assignment operator, the compiler will create a
//	default assignment operator for you. This will cause a problem if your class
//	has a pointer!!!
//Book & Book::operator = ( Book &rhs )
//{
//	m_pName		= rhs.m_pName;
//	m_numPages  = rhs.m_numPages;
//	return *this;
//}

// This is called DEEP COPY
// NOTE: If your class has a pointer, you need to copy the value, NOT the address
//	of the pointer!
Book & Book::operator = ( Book &rhs )
{
	// Paranoid check! Prevent from doing disasterous things by assigning itself to itself...
	if ( this == &rhs )
		return *this;

	//m_pName		= rhs.m_pName; // Wrong way done by the default assignment operator
	// Do it this way
	delete [] m_pName;
	m_pName = new char[ strlen(rhs.m_pName) + 1 ];
	strcpy( m_pName, rhs.m_pName );

	m_numPages  = rhs.m_numPages;
	return *this;
}